package nasif.cn.imagewikis.image.impl;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import nasif.cn.imagewikis.entity.Picture;
import nasif.cn.imagewikis.image.Listener;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WantuImageHttpListener implements Listener{
	private Map<Integer,Picture> map = new HashMap<Integer,Picture>();
	private final String dataDomain = "http://nasif.imagewiki.cn/imagewiki/api/picture/add";
	@Override
	public void handle() {
		WebDriver driver = new FirefoxDriver();
		driver.get("http://wantu.taobao.com/all/hot");
		List<WebElement> list = driver.findElements(By.className("pix-list-content"));
		Integer beforeScroll = list.size();
		JavascriptExecutor jsExecutor = (JavascriptExecutor)driver;
		jsExecutor.executeScript("window.scroll(0,2000)"); 
		Integer afterScroll = list.size();
		Boolean flag = true;
		while(beforeScroll!=afterScroll||flag){
			jsExecutor.executeScript("javascript:window.scrollBy(0,2000)"); 
			beforeScroll = afterScroll;
			list = driver.findElements(By.className("pix-list-content"));
			afterScroll = list.size();
			try {
				for(WebElement element : list){
					try {
						WebElement img = element.findElement(By.tagName("img"));
						String imgUrl = img.getAttribute("src");
						WebElement title = element.findElement(By.className("title"));
						String description = title.getText();
						WebElement detail = element.findElement(By.className("picwrap"));
						String detailUrl = detail.getAttribute("href");
						Picture pictureDO = new Picture();
						pictureDO.setUrl(imgUrl);
						pictureDO.setDescription(description);
						pictureDO.setDetailUrl(detailUrl);
						map.put(pictureDO.hashCode(), pictureDO);
					} catch (Exception e) {
					}
				}
				WebElement next = driver.findElement(By.className("pix-navi-next"));
				next.click();
				HttpClient httpclient = new DefaultHttpClient();
				for(Map.Entry<Integer, Picture> entry : map.entrySet()){
					Picture pictureDO = entry.getValue();
					HttpPost post = new HttpPost(dataDomain);
					List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
					nvps.add(new BasicNameValuePair("url", pictureDO.getUrl()));
					nvps.add(new BasicNameValuePair("detailUrl", pictureDO.getDetailUrl()));
					nvps.add(new BasicNameValuePair("description", pictureDO.getDescription()));
					post.setEntity(new UrlEncodedFormEntity(nvps,"utf-8"));
					HttpResponse response = httpclient.execute(post);
					System.out.println(response.getStatusLine());
					post.releaseConnection();
				}
				httpclient.getConnectionManager().shutdown();
				map.clear();
			} catch(NoSuchElementException e){
				flag = false;
			}catch (Exception e) {
				flag = true;
			}
		}
	}
}
